package com.tuanzi.loan.web.bean.family;

import com.base.common.log.LogService;
import com.tuanzi.loan.business.entity.family.LoanWholeSaleComment;
import com.tuanzi.loan.business.service.family.WholeSaleStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.WholeSaleStatSearchRequest;
import com.tuanzi.loan.vo.response.family.WholeSaleStatSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.WholeSaleStatDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class WholeSaleStatListBean extends JSFListingBean<WholeSaleStatSearchRequest, WholeSaleStatSearchResponse> {
    private static final long serialVersionUID = 5184765941984309704L;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private WholeSaleStatService service;
    @PostConstruct
    public void init(){
        initLazyDataModel();
    }

    private void  initLazyDataModel(){
        $ = new WholeSaleStatSearchRequest();
        Date date = null;
        try {
            date = sdf.parse(sdf.format(new DateTime().toDate()));
        } catch (ParseException e) {
            LogService.error("日期类型转换异常",e);
        }
        $.setStartdate(date);
        $.setEnddate(date);
        $.setC_from("请选择");
        $.setType("请选择");
        lazyDataModel = new WholeSaleStatDataModel($);

    }
    /**
    * @param:           BigDecimal
    * @Description:    对数据转为百分比
    * @Author:         xionghao
    * @CreateDate:     2018-07-25
    */
    public String addPercent(BigDecimal obs){
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(1);
        if (obs.compareTo(BigDecimal.ZERO)!=0)
            return percent.format(obs);
        return String.valueOf(BigDecimal.ZERO);
    }

    public List<String> createCfrinList(){
        List<String> list = service.findCfrom();
        if (list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                if ("null".equals(list.get(i))||"".equals(list.get(i))||null==list.get(i)){
                    list.remove(i);
                }
            }
        }
        return list;
    }

    public List<LoanWholeSaleComment> createType(){
        return service.findLoanWholeSaleComment();
    }
}
